<!-- subpkg_archive/form/index.vue -->
<script setup>
  import { ref } from 'vue'
  import { addPatientApi } from '../../apis/user'
  import { patientDetailApi, updatePatientApi } from '../../apis/patient'
  const isDefault = ref([0])
  // 表单组件 ref
  const formRef = ref()
  //表单数据
  const formData = ref({
    name: '',
    idCard: '',
    gender: 1,
    defaultFlag: 1,
  })

  // 是否为默认就诊人
  function onSwitchChange(ev) {
    // 是否设置为默认就诊患人
    formData.value.defaultFlag = ev.detail.value ? 1 : 0
  }
  // 表单验证规则
  const formRules = {
    name: {
      rules: [
        { required: true, errorMessage: '请填写患者姓名' },
        {
          pattern: '^[\u4e00-\u9fa5]{2,5}$',
          errorMessage: '患者姓名为2-5位中文',
        },
      ],
    },
    idCard: {
      rules: [
        { required: true, errorMessage: '请输入身份证号' },
        {
          pattern:
            '^[1-9]\\d{5}(?:18|19|20)\\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\\d|30|31)\\d{3}[\\dXx]$',
          errorMessage: '身份证号格式不正确',
        },
      ],
    },
    gender: {
      rules: [
        { required: true, errorMessage: '请勾选患者姓名' },
        {
          validateFunction(rule, value, data, callback) {
            // 检测身份证号第17位是否为偶数
            if (data.idCard.slice(16, 17) % 2 !== value) {
              callback('选择的性别与身份号中性别不一致')
            }
            // 验证通过时返回 true
            return true
          },
        },
      ],
    },
  }
  async function fufu() {
    // console.log('当前表单数据:', formData.value)
    // await formRef.value.validate()
    // addPatientApi

    props.id
      ? await updatePatientApi(formData.value)
      : await addPatientApi(formData.value)
    uni.showToast({
      icon: 'success',
      title: props.id ? '编辑患者成功' : '添加患者成功',
    })

    uni.redirectTo({
      url: '/subpkg_archive/list/index',
    })
  }

  import { onLoad } from '@dcloudio/uni-app'

  // 生命周期（页面加载完成）
  onLoad((item) => {
    // console.log(item.id, '111')
  })
  // 使用 defineProps 接收地址参数
  const props = defineProps({ id: String })
  // console.log(props.id, '222')
  const getPatientDetail = async () => {
    uni.showLoading({
      title: '正在加载中....',
    })
    if (!props.id) return
    uni.setNavigationBarTitle({ title: '编辑患者' })
    const res = await patientDetailApi(props.id)
    // console.log(res)
    const { id, gender, idCard, name, defaultFlag } = res.data

    formData.value = { id, gender, idCard, name, defaultFlag }
    // 渲染患者信息
    uni.hideLoading()
  }
  getPatientDetail()
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <uni-forms
        border
        label-width="220rpx"
        :model="formData"
        :rules="formRules"
        ref="formRef"
      >
        <uni-forms-item label="患者姓名" name="name">
          <uni-easyinput
            v-model="formData.name"
            placeholder-style="color: #C3C3C5; font-size: 32rpx"
            :styles="{ color: '#121826' }"
            :input-border="false"
            :clearable="false"
            placeholder="请填写真实姓名"
          />
        </uni-forms-item>
        <uni-forms-item label="患者身份证号" name="idCard">
          <uni-easyinput
            placeholder-style="color: #C3C3C5; font-size: 32rpx"
            :styles="{ color: '#121826' }"
            :input-border="false"
            :clearable="false"
            placeholder="请填写身份证号"
            v-model="formData.idCard"
          />
        </uni-forms-item>
        <uni-forms-item label="患者性别" name="gender">
          <uni-data-checkbox
            selectedColor="#16C2A3"
            v-model="formData.gender"
            :localdata="[
              { text: '男', value: 1 },
              { text: '女', value: 0 },
            ]"
          />
        </uni-forms-item>
        <uni-forms-item label="默认就诊人" name="name">
          <view class="uni-switch">
            <switch
              color="#20c6b2"
              style="transform: scale(0.7)"
              :checked="formData.defaultFlag === 1"
              @change="onSwitchChange"
            />
          </view>
        </uni-forms-item>
        <button class="uni-button" @click="fufu">保存</button>
      </uni-forms>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>
